import {getAllByPage, add, edit, del, start, stop, trigger} from '@/services/xxlJob/XxlJobInfo';
import {message} from 'antd';

export default {
  namespace: 'PMonitorXxlJob',

  state: {
    dataTable: {
      total: 0,
      list: [],
    },
  },

  effects: {
    * getAllByPage({payload}, {call, put}) {
      const response = yield call(getAllByPage, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgDataTable',
          payload: response.data,
        });
      }
    },

    * add({payload, callback}, {call}) {
      const response = yield call(add, payload);
      if (response.code === 0) {
        message.success('添加成功').then();
        callback && callback();
      }
    },

    * edit({payload, callback}, {call}) {
      const response = yield call(edit, payload);
      if (response.code === 0) {
        message.success('修改成功').then();
        callback && callback();
      }
    },

    * del({payload, callback}, {call}) {
      const response = yield call(del, payload);
      if (response.code === 0) {
        message.success('删除成功').then();
        callback && callback();
      }
    },

    * start({payload, callback}, {call}) {
      const response = yield call(start, payload);
      if (response.code === 0) {
        message.success('启动成功').then();
        callback && callback();
      }
    },

    * stop({payload, callback}, {call}) {
      const response = yield call(stop, payload);
      if (response.code === 0) {
        message.success('停止成功').then();
        callback && callback();
      }
    },

    * trigger({payload, callback}, {call}) {
      const response = yield call(trigger, payload);
      if (response.code === 0) {
        message.success('执行成功').then();
        callback && callback();
      }
    },

  },

  reducers: {
    chgDataTable(state, action) {
      return {
        ...state,
        dataTable: action.payload,
      };
    },
  },
};
